package com.linkoog.devtools.json.file_editer.editer.ui.jsonTree;

import com.fasterxml.jackson.databind.node.TextNode;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.ActionPopupMenu;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.ui.TreeSpeedSearch;
import com.intellij.ui.treeStructure.Tree;
import com.linkoog.devtools.json.file_editer.editer.ui.JackJsonTreeNode;
import com.linkoog.devtools.json.file_editer.editer.ui.JackJsonTreeUtils;
import com.linkoog.devtools.json.file_editer.editer.ui.jsonTree.action.CollapseMultiAction;
import com.linkoog.devtools.json.file_editer.editer.ui.jsonTree.action.CopyKeyAction;
import com.linkoog.devtools.json.file_editer.editer.ui.jsonTree.action.CopyKeyValueAction;
import com.linkoog.devtools.json.file_editer.editer.ui.jsonTree.action.CopyValueAction;
import com.linkoog.devtools.json.file_editer.editer.ui.jsonTree.action.ExpandMultiAction;
import com.linkoog.devtools.json.file_editer.editer.ui.jsonTree.action.RemoveAction;

import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class JsonStructureTree extends Tree {

    JackJsonTreeNode rootNode;

    public JsonStructureTree() {
        this(JackJsonTreeNode.DEFAULT);
    }

    public JsonStructureTree(JackJsonTreeNode rootNode) {
        this.rootNode = rootNode;

        this.setModel(new DefaultTreeModel(rootNode));
        //this.setShowsRootHandles(true);
        //this.setRootVisible(false);

        this.setDragEnabled(true);
        this.setExpandableItemsEnabled(true);
        this.setFont(new Font("JetBrains Mono", Font.PLAIN, 12));
        this.setCellRenderer(new JsonStructureTreeCellRender());

        initPopupMenu();
        // 触发快速检索
        TreeSpeedSearch.installOn(this);
    }

    public void reset(String json){
        rootNode = JackJsonTreeUtils.buildJsonTree(json);

        DefaultTreeModel listTreeTableModel = (DefaultTreeModel)getModel();
        listTreeTableModel.setRoot(rootNode);
    }


    private void initPopupMenu() {
        DefaultActionGroup group = new DefaultActionGroup();
        group.addSeparator();
        group.add(new CopyKeyAction(this));
        group.addSeparator();
        group.add(new CopyValueAction(this));
        group.addSeparator();
        group.add(new CopyKeyValueAction(this));
        group.addSeparator();
        group.add(new ExpandMultiAction(this));
        group.addSeparator();
        group.add(new CollapseMultiAction(this));
        group.addSeparator();
        group.add(new RemoveAction(this));
        ActionPopupMenu actionPopupMenu = ActionManager.getInstance().createActionPopupMenu(ActionPlaces.POPUP, group);
        JPopupMenu popupMenu = actionPopupMenu.getComponent();

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                    // 获取选中的节点
                    TreePath[] paths = getSelectionPaths();
                    if (paths != null) {
                        int row = getRowForLocation(e.getX(), e.getY());
                        if (row != -1) {
                            setSelectionRow(row);
                            popupMenu.show(JsonStructureTree.this, e.getX(), e.getY());
                        }
                    } else {
                        popupMenu.show(JsonStructureTree.this, e.getX(), e.getY());
                    }
                }
            }
        });
    }





}
